package cc.rengu.redp.hawk.controller;

import cc.rengu.redp.hawk.service.HawkMonitorService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xushitang
 */
@RestController
@RequestMapping("/hawk/push")
public class HawkPushController {

    @Autowired
    private HawkMonitorService hawkMonitorService;

    /**
     * 指标数据推送接口
     * @param hawkIds 支持同时把指标数据推送到多个图表：id1,id2,id3...
     * @param indexDataJson 指标列表JSON字串： {'指标1':值1, '指标2':值2, '指标3':值3, ...}
     */
    @PostMapping
    public void push(String hawkIds, String indexDataJson) {
        for (String hawkId : hawkIds.split(",")){
            hawkMonitorService.push(hawkId, indexDataJson);
        }
    }

    /**
     * 指标列表推送接口
     * @param indexes [{'hawkId':'id1', 'indexData':{'指标1':值1, '指标2':值2, '指标3':值3, ...}},
     *                {'hawkId':'id2', 'indexData':{'指标1':值1, '指标2':值2, '指标3':值3, ...}},
     *                  ...]
     */
    @PostMapping("list/")
    public void push(String indexes) {
        JSONArray jsonList = JSON.parseArray(indexes);
        jsonList.stream()
                .map(json -> (JSONObject)json)
                .forEach(json -> hawkMonitorService.push(json.getString("hawkId"), json.getJSONObject("indexData")));
    }
}
